package com.ruoyi.vote.app.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.vote.admin.domain.bo.VoteBannerBo;
import com.ruoyi.vote.admin.domain.vo.VoteBannerVo;
import com.ruoyi.vote.admin.domain.vo.VoteCertificateVo;
import com.ruoyi.vote.admin.domain.vo.VotePrizeVo;
import com.ruoyi.vote.admin.service.IVoteBannerService;
import com.ruoyi.vote.admin.service.IVoteCertificateService;
import com.ruoyi.vote.admin.service.IVotePrizeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * API - 公共相关
 * @author 立勤
 * @date 2023/07/18 09:17:14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/saIgnore/common")
public class ApiCommonController extends BaseController {

    private final ISysOssService iSysOssService;
    private final IVoteBannerService bannerService;
    private final IVoteCertificateService iVoteCertificateService;
    private final IVotePrizeService iVotePrizeService;

    /**
     * 获取banner
     * @return
     */
    @GetMapping("/get-banner")
    public TableDataInfo<VoteBannerVo> getBanner(PageQuery pageQuery){
        return bannerService.queryPageList(new VoteBannerBo(), pageQuery);
    }

    /**
     * 上传OSS对象存储
     *
     * @param file 文件
     */
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOssVo oss = iSysOssService.upload(file);
        Map<String, String> map = new HashMap<>(2);
        map.put("url", oss.getUrl());
        map.put("fileName", oss.getOriginalName());
        map.put("ossId", oss.getOssId().toString());
        return R.ok(map);
    }

    /**
     * 获取证书模板详细信息
     *
     * @param id 主键
     */
    @GetMapping("/get-cert/{id}")
    public R<VoteCertificateVo> getCert(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iVoteCertificateService.queryById(id));
    }

    /**
     * 奖品详细信息
     *
     * @param id 主键
     */
    @GetMapping("/get-prize-info/{id}")
    public R<VotePrizeVo> getPrizeInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iVotePrizeService.queryById(id));
    }
}
